#include "processmanager.h"

processmanager::processmanager()
{

}

QString processmanager::getUserName()
{

    QDBusInterface * m_pSystemUserIface = new QDBusInterface("org.freedesktop.Accounts",
                                                             "/org/freedesktop/Accounts",
                                                             "org.freedesktop.Accounts",
                                                             QDBusConnection::systemBus());

    QStringList users;
    QDBusReply<QList<QDBusObjectPath> > reply = m_pSystemUserIface->call("ListCachedUsers");
    if (reply.isValid()) {
        for (QDBusObjectPath op : reply.value())
            users << op.path();
    }

    UserInfomation user;
    for (QString objectpath : users){
         user = GetUserInformation(objectpath);
         if(user.current == true && user.logined == true){
             break;
         }
    }

    return  user.username;
}

UserInfomation processmanager::GetUserInformation(QString objpath)
{

    UserInfomation user;
    //默认值
    user.current = false;
    user.logined = false;
    user.autologin = false;

    QDBusInterface * iproperty = new QDBusInterface("org.freedesktop.Accounts",
                                                    objpath,
                                                    "org.freedesktop.DBus.Properties",
                                                    QDBusConnection::systemBus());

    QDBusReply<QMap<QString, QVariant> > reply = iproperty->call("GetAll", "org.freedesktop.Accounts.User");
    if (reply.isValid()) {
        QMap<QString, QVariant> propertyMap;
        propertyMap = reply.value();
        user.username = propertyMap.find("UserName").value().toString();
        if (user.username == QString(g_get_user_name())) {
            user.current = true;
            user.logined = true;
        }
        user.autologin = false;
    }

    return user;

}
